package com.smart.community.property.vo;

import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 维修工单视图对象
 * @author Wu.Liang
 * @since 2025-06-26
 * @version 1.0.0
 */
@Data
public class RepairOrderVO {

    private Long id;

    /**
     * 工单号
     */
    private String orderNo;

    private Long userId;

    private String userName;

    private Long communityId;

    private String communityName;

    private Long householdId;

    private String householdInfo;

    private String title;

    private String description;

    private String repairAddress;

    private String contactName;

    private String contactPhone;

    private Integer repairType;

    private Integer urgencyLevel;

    private Integer status;

    private BigDecimal estimatedCost;

    private BigDecimal actualCost;

    private Integer satisfactionScore;

    private String userEvaluation;

    private String processRemark;

    private String remark;

    private Long repairmanId;

    private String repairmanName;

    private String repairmanPhone;

    private LocalDateTime submitTime;

    private LocalDateTime acceptTime;

    private LocalDateTime startTime;

    private LocalDateTime expectedCompletionTime;

    private LocalDateTime completionTime;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    /**
     * 维修类型名称（从数据库查询直接获取）
     */
    private String repairTypeName;

    /**
     * 紧急程度名称（从数据库查询直接获取）
     */
    private String urgencyLevelName;

    /**
     * 工单状态名称（从数据库查询直接获取）
     */
    private String statusName;

    /**
     * 优先级名称（从数据库查询直接获取）
     */
    private String priorityName;

    /**
     * 维修图片信息，JSON格式存储多个文件信息
     */
    private String repairImages;

    /**
     * 获取维修类型名称
     */
    public String getRepairTypeName() {
        // 优先使用数据库查询的名称，如果没有则通过代码计算
        if (repairTypeName != null) {
            return repairTypeName;
        }
        
        if (repairType == null) {
            return "未知";
        }
        switch (repairType) {
            case 1:
                return "水电维修";
            case 2:
                return "门窗维修";
            case 3:
                return "电梯维修";
            case 4:
                return "消防维修";
            case 5:
                return "其他";
            default:
                return "未知";
        }
    }

    /**
     * 获取紧急程度名称
     */
    public String getUrgencyLevelName() {
        // 优先使用数据库查询的名称，如果没有则通过代码计算
        if (urgencyLevelName != null) {
            return urgencyLevelName;
        }
        
        if (urgencyLevel == null) {
            return "未知";
        }
        switch (urgencyLevel) {
            case 1:
                return "一般";
            case 2:
                return "紧急";
            case 3:
                return "非常紧急";
            default:
                return "未知";
        }
    }

    /**
     * 获取工单状态名称
     */
    public String getStatusName() {
        // 优先使用数据库查询的名称，如果没有则通过代码计算
        if (statusName != null) {
            return statusName;
        }
        
        if (status == null) {
            return "未知";
        }
        switch (status) {
            case 1:
                return "待受理";
            case 2:
                return "已受理";
            case 3:
                return "维修中";
            case 4:
                return "已完成";
            case 5:
                return "已取消";
            default:
                return "未知";
        }
    }

    /**
     * 计算处理时长（小时）
     */
    public BigDecimal getProcessingTime() {
        if (submitTime == null) {
            return BigDecimal.ZERO;
        }
        LocalDateTime endTime = completionTime != null ? completionTime : LocalDateTime.now();
        long hours = java.time.Duration.between(submitTime, endTime).toHours();
        return new BigDecimal(hours);
    }

    /**
     * 判断是否逾期
     */
    public boolean isOverdue() {
        if (expectedCompletionTime == null || status == null) {
            return false;
        }
        return LocalDateTime.now().isAfter(expectedCompletionTime) && status < 4;
    }
}
